package app.modules.security.application.web;

import app.modules.common.dto.web.WebIdDTO;
import app.modules.common.util.web.PageUtil;
import app.modules.security.business.service.role.RoleFunctionRelationService;
import app.modules.security.business.service.role.RoleService;
import app.modules.security.dto.web.role.WebAssignFunctionDTO;
import app.modules.security.dto.web.role.WebAssignedFunctionDTO;
import app.modules.security.dto.web.role.WebRoleDTO;
import app.modules.security.dto.web.role.WebRoleQueryDTO;
import app.modules.security.dto.web.role.WebRoleSaveDTO;
import app.modules.security.dto.web.role.WebRoleUpdateDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jasmine.framework.web.entity.WebResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;


/**
 * @author mh.z
 */
@Tag(name = "接口管理/角色API")
@RestController
public class RoleController {
    private final RoleService roleService;
    private final RoleFunctionRelationService roleFunctionRelationService;

    public RoleController(RoleService roleService, RoleFunctionRelationService roleFunctionRelationService) {
        this.roleService = roleService;
        this.roleFunctionRelationService = roleFunctionRelationService;
    }

    @Operation(summary = "分页查询角色")
    @PostMapping("/api/security/role/pageRolesByCond")
    public ResponseEntity<WebResult<List<WebRoleDTO>>> pageWebRoleDTOsByCond(
            @RequestBody WebRoleQueryDTO queryDTO) {
        Page page = PageUtil.getPage(queryDTO);

        List<WebRoleDTO> recordList = roleService.pageWebRoleDTOsByCond(queryDTO, page);
        WebResult result = PageUtil.getResult(recordList, page);

        return result.toOkEntity();
    }

    @Operation(summary = "查询指定 ID 的角色")
    @PostMapping("/api/security/role/getRoleById")
    public ResponseEntity<WebResult<WebRoleDTO>> getRoleById(
            @RequestBody WebIdDTO dto) {
        Long id = dto.getId();
        WebRoleDTO roleDTO = roleService.getWebRoleDtoById(id);

        return WebResult.success(roleDTO).toOkEntity();
    }

    @Operation(summary = "保存角色")
    @PostMapping("/api/security/role/saveRole")
    public ResponseEntity<WebResult<WebRoleDTO>> saveRole(
            @Valid @RequestBody WebRoleSaveDTO saveDTO) {
        WebRoleDTO roleDTO = roleService.saveRole(saveDTO);
        WebResult result = WebResult.success(roleDTO);

        return result.toOkEntity();
    }

    @Operation(summary = "更新角色")
    @PostMapping("/api/security/role/updateRole")
    public ResponseEntity<WebResult<WebRoleDTO>> updateRole(
            @Valid @RequestBody WebRoleUpdateDTO updateDTO) {
        WebRoleDTO roleDTO = roleService.updateRole(updateDTO);
        WebResult result = WebResult.success(roleDTO);

        return result.toOkEntity();
    }

    @Operation(summary = "删除指定 ID 的角色")
    @PostMapping("/api/security/role/deleteRoleById")
    public ResponseEntity<WebResult<Object>> deleteRoleById(
            @RequestBody WebIdDTO paramDTO) {
        Long recordId = paramDTO.getId();
        roleService.deleteRecordById(recordId);

        return WebResult.success().toOkEntity();
    }

    @Operation(summary = "查询指定角色的所有已分配功能")
    @PostMapping("/api/security/role/listAssignedFunctions")
    public ResponseEntity<WebResult<List<WebAssignedFunctionDTO>>> listWebAssignedFunctionDTOs(
            @RequestBody WebIdDTO paramDTO) {
        Long recordId = paramDTO.getId();
        List<WebAssignedFunctionDTO> recordList = roleFunctionRelationService.listWebAssignedFunctionDTOs(recordId);

        return WebResult.success(recordList).toOkEntity();
    }

    @Operation(summary = "向指定角色分配功能")
    @PostMapping("/api/security/role/assignFunctions")
    public ResponseEntity<WebResult<Object>> assignFunctions(
            @RequestBody WebAssignFunctionDTO assignDTO) {
        Long roleId = assignDTO.getRoleId();
        List<Long> functionIdList = assignDTO.getFunctionIds();
        roleFunctionRelationService.assignFunctions(roleId, functionIdList);

        return WebResult.success().toOkEntity();
    }

}
